//
//  SafeVC.m
//  BrushFace
//
//  Created by timer_q on 15/12/31.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "SafeVC.h"

#define ScrollTag (20160106)
@interface SafeVC ()<UIScrollViewDelegate>

@end

@implementation SafeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self UIConfig];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)UIConfig{
    [self.navBar setTitle:@"安全保障"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    self.view.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 60, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"safe_right_btn_select"] forState:UIControlStateSelected];
    [rightBtn setImage:[UIImage imageNamed:@"safe_right_btn_default"] forState:UIControlStateNormal];
    rightBtn.tag = ScrollTag+1;
    rightBtn.selected = NO;
    [rightBtn addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar setRightView:rightBtn];
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    scroll.backgroundColor = [UIColor clearColor];
    scroll.pagingEnabled = YES;
    scroll.delegate = self;
    scroll.tag = ScrollTag;
    scroll.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scroll];
    
    [self scroLLViewAddSubViewsWithScroll:scroll];
    
}

- (void)scroLLViewAddSubViewsWithScroll:(UIScrollView *)scroll{
    CGFloat imageW = SCREEN_WIDTH-20;
    CGFloat xspace = (SCREEN_WIDTH-imageW)/2.0;
    CGFloat imageF = imageW/3.0*2;
    scroll.contentSize = CGSizeMake(SCREEN_WIDTH*2, 0);

    UIScrollView *borrowScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, scroll.frame.size.height)];
    borrowScroll.contentSize = CGSizeMake(0, imageF*4+xspace*5);
    borrowScroll.showsVerticalScrollIndicator = NO;
    [scroll addSubview:borrowScroll];
    
    UIScrollView *financeScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, scroll.frame.size.height)];
    financeScroll.contentSize = CGSizeMake(0, imageF*4+xspace*5);
    financeScroll.showsVerticalScrollIndicator = NO;
    [scroll addSubview:financeScroll];
    
    for (int i=0; i<4; i++) {
        UIImageView *borrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(xspace, xspace+(imageF+xspace)*i, imageW, imageF)];
        borrowImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"safe%d",i+1]];
        [borrowScroll addSubview:borrowImage];
        
        UIImageView *financeImage = [[UIImageView alloc] initWithFrame:CGRectMake(xspace, xspace+(imageF+xspace)*i, imageW, imageF)];
        financeImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"safe%d%d",i+1,i+1]];
        [financeScroll addSubview:financeImage];
        
    }
}

#pragma mark - action
- (void)rightButtonClick:(UIButton *)sender{
    UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:ScrollTag];
    if (sender.selected == NO) {
        scroll.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        sender.selected = YES;
    }else{
        scroll.contentOffset = CGPointMake(0, 0);
        sender.selected = NO;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    UIButton *rightBtn = (UIButton *)[self.view viewWithTag:ScrollTag+1];
    if (scrollView.contentOffset.x/SCREEN_WIDTH) {
        rightBtn.selected = YES;
    }else{
        rightBtn.selected = NO;
    }
}
@end
